﻿using RxjhServer.DbClss;
using System;
using System.Data;
using System.Threading;
using System.Windows.Forms;

namespace RxjhServer
{
    public partial class 石头属性调整 : Form
    {
        public 石头属性调整()
        {
            InitializeComponent();
        }

        private void 石头属性调整_Load(object sender, EventArgs e)
        {
            刷新();
        }
        private int 类型还原(string 类型)
        {
            int strResult = 0;
            switch (类型)
            {
                case "物品攻击力":
                    strResult = 1;
                    break;
                case "物品防御力":
                    strResult = 2;
                    break;
                case "生命力":
                    strResult = 3;
                    break;
                case "内功力":
                    strResult = 4;
                    break;
                case "命中率":
                    strResult = 5;
                    break;
                case "回避率":
                    strResult = 6;
                    break;
                case "武功攻击力":
                    strResult = 7;
                    break;
                case "升级成功率":
                    strResult = 9;
                    break;
                case "追加伤害值":
                    strResult = 10;
                    break;
                case "武功防御力":
                    strResult = 11;
                    break;
                case "获得金钱":
                    strResult = 12;
                    break;
                case "死亡损失经验减少":
                    strResult = 13;
                    break;

                case "经验获得":
                    strResult = 15;
                    break;
                default:
                    break;
            }
            return strResult;
        }
        private string 类型命名(int 类型)
        {
            string strResult = "未知属性";
            switch (类型)
            {
                case 1:
                    strResult = "物品攻击力";
                    break;
                case 2:
                    strResult = "物品防御力";
                    break;
                case 3:
                    strResult = "生命力";
                    break;
                case 4:
                    strResult = "内功力";
                    break;
                case 5:
                    strResult = "命中率";
                    break;
                case 6:
                    strResult = "回避率";
                    break;
                case 7:
                    strResult = "武功攻击力";
                    break;
                case 9:
                    strResult = "升级成功率";
                    break;
                case 10:
                    strResult = "追加伤害值";
                    break;
                case 11:
                    strResult = "武功防御力";
                    break;
                case 12:
                    strResult = "获得金钱";
                    break;
                case 13:
                    strResult = "死亡损失经验减少";
                    break;
                case 14:
                    break;
                case 15:
                    strResult = "经验获得";
                    break;
                default:
                    break;
            }
            return strResult;
        }
        public void Set石头属性()
        {
            string sql = string.Format("SELECT * FROM 石头属性效果");
            DataTable dBToDataTable = DbClss.DBA.GetDBToDataTable(sql, "PublicDb");
            if (dBToDataTable != null)
            {
                if (dBToDataTable.Rows.Count == 0)
                {
                    Form1.WriteLine(2, "加载石头数据出错----没有石头数据");
                }
                else
                {
                    World.石头属性调整.Clear();
                    for (int i = 0; i < dBToDataTable.Rows.Count; i++)
                    {
                        石头属性调整类 石头 = new 石头属性调整类();
                        石头.类型 = (int)dBToDataTable.Rows[i]["FLD_TYPE"];
                        石头.数量 = (int)dBToDataTable.Rows[i]["FLD_VALUE"];
                        石头.加减 = (int)dBToDataTable.Rows[i]["FLD_增减"];
                        World.石头属性调整.Add(i, 石头);
                    }
                    //Form1.WriteLine(2, "加载石头石头数据完成" + dBToDataTable.Rows.Count);
                }
                dBToDataTable.Dispose();
            }
        }
        public void 刷新()
        {
            listView2.Items.Clear();

            string sql = string.Format("SELECT * FROM 石头属性效果");
            DataTable table1 = DbClss.DBA.GetDBToDataTable(sql, "PublicDb");

            for (int I = 0; I < table1.Rows.Count; I++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems.Clear();
                item.SubItems[0].Text = table1.Rows[I]["ID"].ToString();
                string 类型 = 类型命名(int.Parse(table1.Rows[I]["FLD_TYPE"].ToString()));
                item.SubItems.Add(类型);
                item.SubItems.Add(table1.Rows[I]["FLD_VALUE"].ToString());
                item.SubItems.Add(table1.Rows[I]["FLD_增减"].ToString());
                listView2.Items.Add(item);
            }
            table1.Dispose();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                写数据(int.Parse(textBox1.Text), int.Parse(textBox2.Text), int.Parse(textBox3.Text));
                //this.button1.Enabled = false;
                //base.Close();
                ListView.SelectedIndexCollection C = listView2.SelectedIndices;
                //string key = listView2.Items[C[0]].SubItems[0].Text;
                //s_id = key;
                listView2.Items[C[0]].SubItems[1].Text = textBox1.Text;
                listView2.Items[C[0]].SubItems[2].Text = textBox2.Text ;
                listView2.Items[C[0]].SubItems[3].Text = textBox3.Text;
                Set石头属性();
            }
            catch
            {
                MessageBox.Show("输入错误,请检查!");
            }
        }

        private void listView2_MouseClick(object sender, MouseEventArgs e)
        {
            ListView.SelectedIndexCollection C = listView2.SelectedIndices;
            //string key = listView2.Items[C[0]].SubItems[0].Text;
            //s_id = key;
            string 类型 = 类型还原(listView2.Items[C[0]].SubItems[1].Text).ToString();
            textBox1.Text = 类型;
            textBox2.Text = listView2.Items[C[0]].SubItems[2].Text;
            textBox3.Text = listView2.Items[C[0]].SubItems[3].Text;
        }

        private int 写数据(int type, int value, int 增减)
        {
            string sqlCommand = string.Format("UPDATE 石头属性效果 SET FLD_增减={0} WHERE FLD_TYPE={1} and FLD_VALUE={2}", 增减, type, value);
            try
            {
                int num = DBA.ExeSqlCommand(sqlCommand, "PublicDb");
                if (num == -1)
                {
                    MessageBox.Show(string.Concat(new object[]
                    {
                        "写入表错误1,请检查|",
                        type,
                        "|",
                        value,
                        "|",
                        增减
                    }));
                    return -1;
                }
            }
            catch
            {
                MessageBox.Show(string.Concat(new object[]
                {
                    "写入表错误2,请检查|",
                    type,
                    "|",
                    value,
                    "|",
                    增减
                }));
            }
            return -1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sqls = string.Format("SELECT FLD_VALUE FROM 石头属性效果 where FLD_VALUE={0}", int.Parse(textBox2.Text));
            DataTable table1 = DbClss.DBA.GetDBToDataTable(sqls, "PublicDb");
            if (table1.Rows.Count > 0)
            {
               
                MessageBox.Show("数量不能重复增加失败");
            }
            else
            {

                string sql = string.Format("INSERT INTO 石头属性效果  (FLD_TYPE,FLD_VALUE, FLD_增减)VALUES({0},{1},{2})", int.Parse(textBox1.Text), int.Parse(textBox2.Text), int.Parse(textBox3.Text));
                int num = DbClss.DBA.ExeSqlCommand(sql, "PublicDb");
                if (num == -1)
                {
                    MessageBox.Show(string.Concat(new object[]
                    {
                        "写入表错误1,请检查"
                    }));
                    return;
                }
                else
                {

                    MessageBox.Show(string.Concat(new object[]
              {
                        "增加成功"
              }));
                }
                Set石头属性();
                刷新();
            }
            table1.Dispose();
        }
    }
}
